package sfeir.authform.server.authservice.standard.entity;

import java.util.HashMap;
import java.util.Map;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import sfeir.authform.client.dao.AuthUser;

@PersistenceCapable(identityType = IdentityType.DATASTORE, detachable="true")
public class UserEntity {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Long id;
    @Persistent
    private String email;
    @Persistent
    private String nickname;
    @Persistent
    private String password;
    @Persistent
    private String authDomain;
    @Persistent(serialized="true")
    private Map<String, Object> otherField;

    public UserEntity() {
        super();
        otherField = new HashMap<String, Object>();
    }

    public UserEntity(Long id, String email, String nickname) {
        super();
        this.id = id;
        this.email = email;
        this.nickname = nickname;
        otherField = new HashMap<String, Object>();
    }

    public UserEntity(AuthUser user) {
        super();
        fromUser(user);
    }
    

    public void setId(Long id) {
        this.id = id;
    }

    public Long getId() {
        return id;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public String getNickname() {
        return nickname;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getAuthDomain() {
        return authDomain;
    }

    public void setAuthDomain(String authDomain) {
        this.authDomain = authDomain;
    }

    public void setField(String name, Object value) {
        otherField.put(name, value);
    }

    public Object getField(String name) {
        return otherField.get(name);
    }

    public AuthUser toUser() {
        AuthUser user = new AuthUser(id, email, nickname);
        user.setPassword(password);
        user.setAuthDomain(authDomain);
        user.setOtherField(otherField);
        return user;
    }
    
    public void fromUser(AuthUser user) {
        this.email = user.getEmail();
        this.nickname = user.getNickname();
        this.password = user.getPassword();
        this.authDomain = user.getAuthDomain();
        this.otherField = user.getOtherField();
    }
}
